// HttpUtil.js
const BASE_URL = 'http://10.10.81.13:8101'; // 替换为你的 API 地址

const defaultOptions = {
    headers: {
        'Content-Type': 'application/json',
        // 'Authorization': `Bearer ${localStorage.getItem('token')}`
    },
};

export const Http = {
    get(url) {
        return fetch(`${BASE_URL}${url}`, {
            method: 'GET',
            headers: defaultOptions.headers,
            credentials: 'include'
        }).then(this._handleResponse);
    },

    post(url, body) {
        return fetch(`${BASE_URL}${url}`, {
            method: 'POST',
            headers: defaultOptions.headers,
            body: JSON.stringify(body),
            credentials: 'include'
        }).then(this._handleResponse);
    },

    put(url, body) {
        return fetch(`${BASE_URL}${url}`, {
            method: 'PUT',
            headers: defaultOptions.headers,
            body: JSON.stringify(body),
            credentials: 'include'
        }).then(this._handleResponse);
    },

    delete(url) {
        return fetch(`${BASE_URL}${url}`, {
            method: 'DELETE',
            headers: defaultOptions.headers,
            credentials: 'include'
        }).then(this._handleResponse);
    },

    _handleResponse(response) {
        if (!response.ok) {
            throw new Error(`HTTP 错误！状态码: ${response.status}`);
        }
        return response.json();
    }
};